package com.vercon.moensmart.utils;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;

import androidx.annotation.Nullable;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.vercon.moensmart.R;


public class FrescoUtil {


    /**
     * 构建、获取ImageRequest
     * @param uri 加载路径
     * @param simpleDraweeView 加载的图片控件
     * @return ImageRequest
     */

    public static ImageRequest getImageRequest(Uri uri, SimpleDraweeView simpleDraweeView) {
        int width;
        int height;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            width = simpleDraweeView.getWidth();
            height = simpleDraweeView.getHeight();
        } else {
            width = simpleDraweeView.getMaxWidth();
            height = simpleDraweeView.getMaxHeight();
        }
        //根据请求路径生成ImageRequest的构造者
        ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(uri);
        //调整解码图片的大小
        if (width > 0 && height > 0) {
            builder.setResizeOptions(new ResizeOptions(width, height));
        }
        //设置是否开启渐进式加载，仅支持JPEG图片
        builder.setProgressiveRenderingEnabled(true);
        //图片变换处理
       // CombinePostProcessors.Builder processorBuilder = new CombinePostProcessors.Builder();
        //加入模糊变换
        //processorBuilder.add(new BlurPostprocessor(context, radius));
        //加入灰白变换
       // processorBuilder.add(new GrayscalePostprocessor());
        //应用加入的变换
        //builder.setPostprocessor(processorBuilder.build());
        //更多图片变换请查看https://github.com/wasabeef/fresco-processors
        return builder.build();
    }

    /**
     * 构建、获取Controller
     * @param request
     * @param oldController
     * @return
     */
    public static DraweeController getController(ImageRequest request, @Nullable DraweeController oldController) {
        PipelineDraweeControllerBuilder builder = Fresco.newDraweeControllerBuilder();
        builder.setImageRequest(request);//设置图片请求
        builder.setTapToRetryEnabled(false);//设置是否允许加载失败时点击再次加载
        builder.setAutoPlayAnimations(true);//设置是否允许动画图自动播放
        builder.setOldController(oldController);
        return builder.build();
    }

    /**
     * 加载图片核心方法
     *
     * @param simpleDraweeView 图片加载控件
     * @param uri 图片加载地址
     */
    public static void loadImage(SimpleDraweeView simpleDraweeView, Uri uri) {
        //设置Hierarchy
       // setHierarchay(simpleDraweeView.getHierarchy());
        //构建并获取ImageRequest
        ImageRequest imageRequest = getImageRequest(uri, simpleDraweeView);
        //构建并获取Controller
        DraweeController draweeController = getController(imageRequest, simpleDraweeView.getController());
        //开始加载
        simpleDraweeView.setController(draweeController);
    }

    /**
     * 圆角图片
     * @param simpleDraweeView
     * @param uri
     * @param context
     */
    public static void loadRoundedImage(SimpleDraweeView simpleDraweeView, int radius, Uri uri, Context context) {

        GenericDraweeHierarchy hierarchy = new GenericDraweeHierarchyBuilder(context.getResources())
                .setFadeDuration(10)
                .setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY)
                .build();
        // 设置图片圆角
        RoundingParams roundingParams = new RoundingParams();
        roundingParams.setRoundAsCircle(false); // 不将图片剪切成圆形
        roundingParams.setOverlayColor(Color.parseColor("#00FFFFFF"));
        roundingParams.setCornersRadii(radius,radius,0,0);
        hierarchy.setRoundingParams(roundingParams);
        // 为SimpleDraweeView设置属性
        simpleDraweeView.setHierarchy(hierarchy);
        loadImage(simpleDraweeView,uri);
    }


    /**
     * 圆形图片
     * @param simpleDraweeView
     * @param context
     */
    public static void loadCircleImage(SimpleDraweeView simpleDraweeView, Uri uri, Context context) {

        GenericDraweeHierarchy hierarchy = new GenericDraweeHierarchyBuilder(context.getResources())
                .setPlaceholderImage(R.mipmap.ic_user_add)
                .setFadeDuration(10)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_INSIDE)
                .build();
        // 设置图片圆角
        RoundingParams roundingParams = new RoundingParams();
        roundingParams.setRoundAsCircle(true); // 将图片剪切成圆形
        hierarchy.setRoundingParams(roundingParams);
        // 为SimpleDraweeView设置属性
        simpleDraweeView.setHierarchy(hierarchy);
        loadImage(simpleDraweeView,uri);
    }

    /**
     * 圆形图片
     * @param simpleDraweeView
     * @param context
     */
    public static void loadDetailsCircleImage(SimpleDraweeView simpleDraweeView, Uri uri, Context context) {

        GenericDraweeHierarchy hierarchy = new GenericDraweeHierarchyBuilder(context.getResources())
                .setPlaceholderImage(R.mipmap.ic_detalis_photo)
                .setFadeDuration(10)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_INSIDE)
                .build();
        // 设置图片圆角
        RoundingParams roundingParams = new RoundingParams();
        roundingParams.setRoundAsCircle(true); // 将图片剪切成圆形
        hierarchy.setRoundingParams(roundingParams);
        // 为SimpleDraweeView设置属性
        simpleDraweeView.setHierarchy(hierarchy);
        loadImage(simpleDraweeView,uri);
    }
}